'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    let result = await this.app.mysql.get('blog_content', {})
    this.ctx.body = result
  }
  // 获取首页文章列表
  async getArticleList() {
    let sql = 'SELECT article.id as id,' +
      'article.title as title,' +
      'article.introduce as introduce,' +
      // "FROM_UNIXTIME(article.addTime, '%Y-%m-%d %H:%i:%s') as addTime" +  // 日期格式化
      'article.addTime as addTime,' +
      'article.view_count as view_count,' +
      'article.part_count as part_count,' +
      'article.content as content,' +
      'type.typeName as typeName ' +
      'FROM article LEFT JOIN type ON article.type_id = type.Id'

    const result = await this.app.mysql.query(sql)
    this.ctx.body = {
      data: result
    }
  }
  // 获取文章详情
  async getArticleById() {
    let id = this.ctx.params.id
    let sql = 'SELECT article.id as id,' +
      'article.title as title,' +
      'article.introduce as introduce,' +
      'article.content as content,' +
      // "FROM_UNIXTIME(article.addTime, '%Y-%m-%d %H:%i:%s') as addTime" +  // 日期格式化
      'article.addTime as addTime,' +
      'article.view_count as view_count,' +
      'article.part_count as part_count,' +
      'type.typeName as typeName,' +
      'type.id as typeId ' +
      'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
      'WHERE article.id=' + id

    const result = await this.app.mysql.query(sql)
    this.ctx.body = { data: result }
  }
  // 获取文章类别信息
  async getTypeInfo() {
    const result = await this.app.mysql.select('type')
    this.ctx.body = { data: { result } }
  }
  // 根据类别ID获取文章列表
  async getListById() {
    let id = this.ctx.params.id
    let sql = 'SELECT article.id as id,' +
      'article.title as title,' +
      'article.introduce as introduce,' +
      // "FROM_UNIXTIME(article.addTime, '%Y-%m-%d %H:%i:%s') as addTime" +  // 日期格式化
      'article.addTime as addTime,' +
      'article.view_count as view_count,' +
      'article.part_count as part_count,' +
      'type.typeName as typeName ' +
      'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
      'WHERE type_id=' + id
      const result = await this.app.mysql.query(sql)
      this.ctx.body = {data: result}
  }
}

module.exports = HomeController;
